package com.szh.zuoye;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private EditText mainUsername;
    private EditText mainUserPwd;
    private Button mainBtnLogin;
    private MyReceiver myReceiver;
    MyDBHelper helper;
    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainUsername = (EditText) findViewById(R.id.main_username);
        mainUserPwd = (EditText) findViewById(R.id.main_userPwd);
        mainBtnLogin = (Button) findViewById(R.id.main_btnLogin);

         helper= new MyDBHelper(this);
        db=helper.getWritableDatabase();
        initReceiver();
        final Intent intent = new Intent();
        intent.putExtra("msg", "接受动态广播成功");
        intent.setAction("com.szh.zuoye");
        mainBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = mainUsername.getText().toString();
                String pwd = mainUserPwd.getText().toString();
                Log.d(TAG, "name: "+name);
                Log.d(TAG, "pwd: "+pwd);
                intent.putExtra("name",name);
                intent.putExtra("pwd",pwd);
                sendBroadcast(intent);
            }
        });

    }
    private void initReceiver() {
        myReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.szh.zuoye");
        registerReceiver(myReceiver,filter);
    }
}
